/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import ar.uba.fi.posgrado.academy.model.Body;

/**
 * Usuario
 * 
 * @author nicolas.gonzalez
 * 
 */
@Entity
@Table(name = "USUARIO")
@SuppressWarnings("serial")
public class User implements Serializable {

	@Id
	@Column(name = "NOMBRE_USUARIO", length = 45, unique = true, updatable = false)
	private String name;

	@Column(name = "PASSWORD", length = 45)
	private String password;

	@Column(name = "NOMBRE_COMPLETO", length = 50)
	private String realName;

	@Column(name = "APELLIDO", length = 50)
	private String surname;

	@OneToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "USUARIO_ROL", joinColumns = @JoinColumn(name = "NOMBRE_USUARIO", referencedColumnName = "NOMBRE_USUARIO"), inverseJoinColumns = @JoinColumn(name = "NOMBRE_ROL", referencedColumnName = "NOMBRE_ROL"))
	private Set<Role> roles = new HashSet<Role>();

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_ENTE")
	private Body body;

	/**
	 * @return Devuelve el nombre y apellido
	 */
	public String getFullName() {
		return (this.realName + " " + this.surname);
	}
	
	/**
	 * @return true Cuando el usuario  tiene el rol <b>ente</b>
	 */
	public boolean hasBodyRole() {
		Role role = new Role();
		role.setName("ente");
		return roles.contains(role);
	}
	
	/**
	 * @return true Cuando el usuario  tiene el rol <b>administrador</b>
	 */
	public boolean hasAdminRole() {
		Role role = new Role();
		role.setName("administrador");
		return roles.contains(role);
	}

	/**
	 * @return the body
	 */
	public Body getBody() {
		return body;
	}

	/**
	 * @param body
	 *            the body to set
	 */
	public void setBody(Body body) {
		this.body = body;
	}

	public User() {

	}

	/**
	 * @param name
	 */
	public User(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SIMPLE_STYLE);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this,
				new String[] { this.name });
	}

	@Override
	public boolean equals(final Object other) {
		return EqualsBuilder.reflectionEquals(this, other,
				new String[] { this.name });
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return this.password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the roles
	 */
	public Set<Role> getRoles() {
		return this.roles;
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * Agrega un rol
	 * 
	 * @param role
	 */
	public void addRole(Role role) {
		if (this.roles == null) {
			this.roles = new HashSet<Role>();
		}
		this.roles.add(role);
	}

	/**
	 * @return the realName
	 */
	public String getRealName() {
		return this.realName;
	}

	/**
	 * @param realName
	 *            the realName to set
	 */
	public void setRealName(String realName) {
		this.realName = realName;
	}

	/**
	 * @return the surname
	 */
	public String getSurname() {
		return this.surname;
	}

	/**
	 * @param surname
	 *            the surname to set
	 */
	public void setSurname(String surname) {
		this.surname = surname;
	}

}
